Pasting things from clipboard into Nvim
I don't wanna fork kickstart.nvim
-the-repo, just copy the main file. How hard can it be? So, I do the classic Ctrl-C on the entire raw file in my browser. I know that in vim, you paste by pressing p
, so I hit it.
...aaaand it pasted some random line I dd
'd earlier. Oops!
I could run off to search on the internet, and I'm sure I'd find something, buuuut just being told a magic key sequence won't teach me anything anything. So it's time to, as they say, RTFM.
:help paste
:help paste
"Paste" is a separate concept from |clipboard|: paste means "dump a bunch of
text to the editor", whereas clipboard provides features like |quote+| to get
and set the OS clipboard directly. For example, middle-click or CTRL-SHIFT-v
(macOS: CMD-v) in your terminal is "paste", not "clipboard": the terminal
application (Nvim) just gets a stream of text, it does not interact with the
clipboard directly.
Huh. Fair.
:help clipboard
:help clipboard
Nvim has no direct connection to the system clipboard. Instead it depends on
a |provider| which transparently uses shell commands to communicate with the
system clipboard or any other clipboard "backend".
To ALWAYS use the clipboard for ALL operations (instead of interacting with
the "+" and/or "*" registers explicitly):
vim set clipboard+=unnamedplus
Well, there's a solution to my problem. "ALWAYS use the clipboard for ALL operations" seems a little bit like a trap though, so instead, tell me more about those "registers" of yours.
:help registers
:help registers
There are ten types of registers:
- The unnamed register ""
- 10 numbered registers "0 to "9
- The small delete register "-
- 26 named registers "a to "z or "A to "Z
- Three read-only registers ":, "., "%
- Alternate buffer register "#
- The expression register "=
- The selection registers "* and "+
- The black hole register "_
- Last search pattern register "/
"The black hole register" made me chuckle, but my focus is on the other ones. Reading more, "the unnamed register" is used by default for all copy/pasting needs; while +
and *
have to do with the clipboard. After searching around a bit more in the docs, I found out that "{c}
(where {c}
is the name of the register you want to use) can be prefixed before the various delete/yank/paste operations to use the given register.
So... "+p
. And yeah, that did it! So the +
register has my clipboard contents. Shortly after I realized that means that I can yank into "+
too, which you can see by the fact that I've been copy/pasting manual segments out of nvim onto here.
I don't think I ever consciously interacted with registers at all in my previous dealings. I would just... dump text in in insert mode and hope for the best. So, learning already.